package com.hk.modules.folder.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hk.common.utils.StringUtils;
import com.hk.modules.circuit.pojo.CircuitPojo;
import com.hk.modules.circuit.service.CircuitService;
import com.hk.modules.folder.pojo.CopyFileVo;
import com.hk.modules.folder.pojo.FileVo;
import com.hk.modules.folder.pojo.FolderPojo;
import com.hk.modules.folder.mapper.FolderMapper;
import com.hk.modules.folder.service.FolderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zy
 * @since 2024-04-28
 */
@Service
public class FolderServiceImpl extends ServiceImpl<FolderMapper, FolderPojo> implements FolderService {

    @Autowired
    private CircuitService circuitService;

    @Override
    public void deleteObj(FolderPojo p) {
        if(!StringUtils.isEmpty(p.getId())){
            removeById(p.getId());
            QueryWrapper<CircuitPojo> queryCircuit = new QueryWrapper<>();
            List<CircuitPojo> circuitList = circuitService.list(queryCircuit);
            for (CircuitPojo item: circuitList) {
                circuitService.removeCircuit(item.getId());
            }
        }
    }

    @Override
    public List<FileVo> getFileArr(FolderPojo p) {
        List<FileVo> list = new ArrayList<>();
        QueryWrapper queryFolder = new QueryWrapper<>();
        queryFolder.orderByAsc("name");
        // 是否有ID
        if(p==null||StringUtils.isEmpty(p.getId())){
            queryFolder.isNull("parent_id");
            queryFolder.or();
            queryFolder.eq("parent_id","");
        }else{
            queryFolder.eq("parent_id",p.getId());
        }
        List<FolderPojo> folderList = list(queryFolder);
        for (FolderPojo item: folderList) {
            list.add(new FileVo().setId(item.getId()).setName(item.getName())
                    .setType("folder").setParentId(item.getParentId()));
        }
        List<CircuitPojo> circuitList = circuitService.list(queryFolder);
        for (CircuitPojo item: circuitList) {
            list.add(new FileVo().setId(item.getId()).setName(item.getName())
                    .setType("circuit").setParentId(item.getParentId()));
        }
        return list;
    }

    @Override
    public int copyFile(CopyFileVo p) {
        if(p!=null&&p.getDataArr()!=null&&p.getDataArr().size()>0){
            List<CircuitPojo> circuitArr = new ArrayList<>();
            List<FolderPojo> folderArr = new ArrayList<>();
            for (FileVo fileVo: p.getDataArr()) {
                if("folder".equals(fileVo.getType())){
                    folderArr.add(new FolderPojo().setId(fileVo.getId())
                            .setParentId(p.getTargetId()));
                }else{
                    circuitArr.add(new CircuitPojo().setId(fileVo.getId())
                            .setParentId(p.getTargetId()));
                }
            }
            circuitService.updateBatchById(circuitArr);
            updateBatchById(folderArr);
            return  p.getDataArr().size();
        }
        return 0;
    }
}
